
package com.xebest.web.controller.restapi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.xebest.api.dto.ManagerFinSalesBillDetailPageDto;
import com.xebest.api.dto.ManagerFinSalesBillPageDto;
import com.xebest.api.dto.XeVenFinSalesBillDTO;
import com.xebest.api.dto.XeVenFinSalesBillDeatilDTO;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.pagedto.XePageInfoDto;
import com.xebest.constant.XeErrorCode;
import com.xebest.web.form.XeVenFinSalesBillDetailForm;
import com.xebest.web.form.XeVenFinSalesBillForm;
import com.xebest.web.service.XeVenFinSalesBillDetailService;
import com.xebest.web.service.XeVenFinSalesBillService;
import com.xebest.web.view.XeVenFinSalesBillDetailView;
import com.xebest.web.view.XeVenFinSalesBillView;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 付款单同步接口
 *
 * @author Wangyp
 */
@RestController
@RequestMapping("restapi/finSalesBill")
public class XeVenFinSalesBillRestApi extends BaseController {

    private static Logger log = LoggerFactory.getLogger(XeVenFinSalesBillRestApi.class);

    @Autowired
    private XeVenFinSalesBillService xeVenFinSalesBillService;//付款单操作service
    @Autowired
    private XeVenFinSalesBillDetailService xeVenFinSalesBillDetailService; //付款单商品明细操作service


    /**
     * 收款单列表同步接口
     *
     * @author Wangyp
     */
    @RequestMapping("/listFinSalseBill")
    @ResponseBody
    public ManagerFinSalesBillPageDto listFinSalseBill(@RequestBody XeVenFinSalesBillDTO billDTO) {
        ManagerFinSalesBillPageDto pageDto = new ManagerFinSalesBillPageDto();
        try {
            if(null != billDTO){
                // 分页查询
                PageHelper.startPage(billDTO.getXePageDto().getPageNum(), billDTO.getXePageDto().getPageSize());
            }
            String formJson = JSONObject.toJSONString(billDTO);
            log.info("商户应付款账单列表查询条件：{}", formJson);
            // 查询条件封装
            XeVenFinSalesBillForm param = JSONObject.parseObject(formJson, XeVenFinSalesBillForm.class);

            setSendTime(param);
            // 查询商户应付款账单信息
            List<XeVenFinSalesBillView> resultList = xeVenFinSalesBillService.queryXeVenFinSalesBillList(param);
            List<XeVenFinSalesBillDTO> salesBillList = JSONArray.parseArray(JSONArray.toJSONString(resultList),
                    XeVenFinSalesBillDTO.class);
            //查询商户应付款账单信息
            // 分页信息
            //XePageInfoDto<XeVenFinSalesBillDTO> pageInfo = new XePageInfoDto<XeVenFinSalesBillDTO>(salesBillList);
            XePageInfoDto pageInfo = new XePageInfoDto<>(resultList);
            pageDto.setList(salesBillList);
            pageDto.setXePageInfoDto(pageInfo);
            pageDto.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.error("商户应付款账单接口异常：异常信息{}", e);
            pageDto.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return pageDto;
    }

    /**
     * 设置查询时间
     *
     * @param form
     */
    private void setSendTime(XeVenFinSalesBillForm form) {
        if (null != form.getSearchTimeType()) {
            switch (form.getSearchTimeType()) {
                case 1:
                    break;
                case 2:
                    form.setStartReceiveTime(form.getEndReceiveTime());
                    form.setEndReceiveTime(form.getStartReceiveTime());
                    form.setStartSendTime(null);
                    form.setEndSendTime(null);
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 收款单商品明细列表同步接口
     *
     * @author Wangyp
     */
    @RequestMapping("/listFinSalesBillDetail")
    @ResponseBody
    public ManagerFinSalesBillDetailPageDto listFinSalesBillDetail(@RequestBody XeVenFinSalesBillDeatilDTO billDTO) {
        ManagerFinSalesBillDetailPageDto pageDto = new ManagerFinSalesBillDetailPageDto();
        try {
            String formJson = JSONObject.toJSONString(billDTO);
            log.info("商户应付款账单列表查询条件：{}", formJson);
            // 查询条件封装
            XeVenFinSalesBillDetailForm param = JSONObject.parseObject(formJson, XeVenFinSalesBillDetailForm.class);
            // 查询付款单商品明细
            List<XeVenFinSalesBillDetailView> resultList = xeVenFinSalesBillDetailService.queryXeVenFinSalesBillDetailList(param);
            List<XeVenFinSalesBillDeatilDTO> salesBillList = JSONArray.parseArray(JSONArray.toJSONString(resultList),
                    XeVenFinSalesBillDeatilDTO.class);
            pageDto.setList(salesBillList);
            pageDto.setStatus(XeErrorCode.PORJECT_SUCCESS);
        } catch (Exception e) {
            log.error("付款商品明细接口异常：异常信息{}", e);
            pageDto.setStatus(XeErrorCode.PORJECT_ERROR);
        }
        return pageDto;
    }

}

